package com.eastfair.imaster.service.impl;

import cn.hutool.core.bean.BeanUtil;
import com.baomidou.dynamic.datasource.annotation.DS;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.UpdateWrapper;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.baomidou.mybatisplus.extension.toolkit.SqlHelper;
import com.eastfair.annotation.base.SubjectTypeEnum;
import com.eastfair.boot.request.PageParams;
import com.eastfair.boot.service.SuperCacheServiceImpl;
import com.eastfair.cache.operations.BulletScreenCacheKeyBuilder;
import com.eastfair.constant.BusinessConstant;
import com.eastfair.core.base.R;
import com.eastfair.core.context.ContextUtil;
import com.eastfair.core.model.CacheKeyBuilder;
import com.eastfair.core.snowflake.DefaultUidGenerator;
import com.eastfair.core.utils.ConvertUtil;
import com.eastfair.core.utils.StringUtils;
import com.eastfair.imaster.dao.BulletScreenMapper;
import com.eastfair.imaster.dto.BulletScreenDTO;
import com.eastfair.imaster.dto.BulletScreenPageQuery;
import com.eastfair.imaster.entity.BulletScreen;
import com.eastfair.imaster.service.BulletScreenService;
import com.eastfair.imaster.vo.BulletScreenVO;
import com.eastfair.projectcore.api.ProjectCoreServiceFeign;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;

import java.util.List;

/**
 * <p>
 * 业务实现类
 *
 * </p>
 *
 * @author zjn
 * @date 2021-08-31
 */
@Slf4j
@Service
@DS("#thread.tenant")
public class BulletScreenServiceImpl extends SuperCacheServiceImpl<BulletScreenMapper, BulletScreen> implements BulletScreenService {

    @Autowired
    ProjectCoreServiceFeign projectCoreServiceFeign;

    @Override
    protected CacheKeyBuilder cacheKeyBuilder() {
        return new BulletScreenCacheKeyBuilder();
    }

    /**************sharding 分库分表操作 *****************/
    @Override
    public boolean updateByIdOfShard(BulletScreen entity) {
        UpdateWrapper<BulletScreen> updateWrapper = new UpdateWrapper<>();
        //可将指定字段更新为null
        updateWrapper.lambda().eq(BulletScreen::getProjectId, entity.getProjectId()).eq(BulletScreen::getId, entity.getId());
        entity.setProjectId(null);
        //删除缓存
        delCache(entity);
        return SqlHelper.retBool(this.getBaseMapper().update(entity, updateWrapper));
    }

    /**
     * 处理新增相关处理
     *
     * @param model 实体
     * @return 是否成功
     */
    @Override
    protected R<Boolean> handlerSave(BulletScreen model) {
        //雪花ID
        model.setId(DefaultUidGenerator.getUidOfProjectId(ContextUtil.getProjectId()));
        model.setProjectId(ContextUtil.getProjectId());
        return R.successDef();
    }

    @Override
    public Boolean deleteById(Long id) {
        BulletScreen bulletScreen = new BulletScreen();
        bulletScreen.setId(id);
        bulletScreen.setProjectId(ContextUtil.getProjectId());
        bulletScreen.setIsDeleted(BusinessConstant.DELETE_YES);
        return updateByIdOfShard(bulletScreen);
    }

    @Override
    public Boolean updateById(BulletScreenDTO bulletScreenDTO) {
        BulletScreen bulletScreen = BeanUtil.copyProperties(bulletScreenDTO, BulletScreen.class);
        bulletScreen.setProjectId(ContextUtil.getProjectId());
        return updateByIdOfShard(bulletScreen);
    }

    @Override
    public BulletScreenVO queryById(Long id) {
        QueryWrapper<BulletScreen> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BulletScreen::getProjectId, ContextUtil.getProjectId()).eq(BulletScreen::getId, id);
        BulletScreen bulletScreen = getOne(wrapper, false);
        return BeanUtil.copyProperties(bulletScreen, BulletScreenVO.class);
    }

    @Override
    public Page<BulletScreenVO> queryPageList(PageParams<BulletScreenPageQuery> params) {
        QueryWrapper<BulletScreen> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BulletScreen::getProjectId, ContextUtil.getProjectId())
                .eq(BulletScreen::getIsDeleted, BusinessConstant.DELETE_NO)
                .orderByDesc(BulletScreen::getUpdateTime);

        if (StringUtils.isBlank(params.getModel().getSubjectType())) {
            wrapper.lambda().eq(BulletScreen::getSubjectType, SubjectTypeEnum.get(params.getModel().getSubjectType()).name());
        }

        Page<BulletScreen> bulletScreenPage = page(params.buildPage(), wrapper);
        return ConvertUtil.convertPage(bulletScreenPage, BulletScreenVO.class);
    }

    @Override
    public List<BulletScreenVO> queryList(BulletScreenDTO bulletScreenDTO) {
        QueryWrapper<BulletScreen> wrapper = new QueryWrapper<>();
        wrapper.lambda().eq(BulletScreen::getProjectId, ContextUtil.getProjectId())
                .eq(BulletScreen::getIsDeleted, BusinessConstant.DELETE_NO).eq(BulletScreen::getIsEnabled, BusinessConstant.ENABLE_YES)
                .eq(BulletScreen::getSubjectType, ContextUtil.getSubjectType())
                .orderByDesc(BulletScreen::getUpdateTime);
        List<BulletScreen> result = list(wrapper);
        return ConvertUtil.convertList(result, BulletScreenVO.class);
    }

}
